package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.ProjectPayment;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.customer.SuppliersAssessService;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ContractService;
import com.xpsoft.oa.service.project.ProjectPaymentService;
import com.xpsoft.oa.service.project.ProjectService;

public class ProjectPaymentFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(ProjectPaymentFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String contractId = (String)pi.getVariable("projectPayment.contract.id");
		String proId = (String)pi.getVariable("projectPayment.project.id");
		Date formDate = (Date)pi.getVariable("projectPayment.formDate");
		String suppliersId = (String)pi.getVariable("projectPayment.suppliersAssess.suppliersId");
		String contactName = (String)pi.getVariable("projectPayment.contactName");
		String contactTel = (String)pi.getVariable("projectPayment.contactTel");
		String bankName = (String)pi.getVariable("projectPayment.bankName");
		String bankNo = (String)pi.getVariable("projectPayment.bankNo");
		String payType = (String)pi.getVariable("projectPayment.payType");
		BigDecimal contractPercentage = (BigDecimal)pi.getVariable("projectPayment.contractPercentage");
		BigDecimal applyPercentage = (BigDecimal)pi.getVariable("projectPayment.applyPercentage");
		BigDecimal applyAmount = (BigDecimal)pi.getVariable("projectPayment.applyAmount");
		String applyAmountBig = (String)pi.getVariable("projectPayment.applyAmountBig");
		String billInfo = (String)pi.getVariable("projectPayment.billInfo");
		
		ProjectPayment projectPayment = new ProjectPayment();
		projectPayment.setFormDate(formDate);
		projectPayment.setContactName(contactName);
		projectPayment.setContactTel(contactTel);
		projectPayment.setBankName(bankName);
		projectPayment.setBankNo(bankNo);
		projectPayment.setPayType(payType);
		projectPayment.setContractPercentage(contractPercentage);
		projectPayment.setApplyAmount(applyAmount);
		projectPayment.setApplyAmountBig(applyAmountBig);
		projectPayment.setApplyPercentage(applyPercentage);
		projectPayment.setBillInfo(billInfo);
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			projectPayment.setProject(projectService.get(Long.valueOf(proId)));
		}
		if(StringUtils.isNotBlank(contractId)){
			ContractService contractService = (ContractService)AppUtil.getBean("contractService");
			projectPayment.setContract(contractService.get(Long.valueOf(contractId)));
		}
		if(StringUtils.isNotBlank(suppliersId)){
			SuppliersAssessService suppliersAssessService = (SuppliersAssessService)AppUtil.getBean("suppliersAssessService");
			projectPayment.setSuppliersAssess(suppliersAssessService.get(Long.valueOf(suppliersId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		projectPayment.setProcessRunId(processRun.getRunId());
		
		ProjectPaymentService projectPaymentService = (ProjectPaymentService)AppUtil.getBean("projectPaymentService");
		projectPaymentService.save(projectPayment);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
